Hi Tony,
Driving a servo into a hard stop can be tricky. I tried to understand what you were trying to do with the program. It seems a large move is commanded at some speed that will hit the stop. Then as it is moving the encoder position is sampled every 50ms and if it doesn't change by more than 5 counts it is assumed we hit the stop and we command to servo where we currently are.
You forgot to specify what the variable Conv_to_Cycles is, but from an earlier email I see it was:
#define Conv_to_Cycles 41615.36
So the commanded velocity is:
41615.36/60 * 20 = 13871 counts/sec
The amount moved every 0.05 sec would be 693 counts per sample
You might consider sampling faster. 10X faster every 0.005 sec would still nominally move ~70 counts per sample.
But depending on how your servo is tuned it may not like servoing while at or in a hard stop. The main issue with doing this is usually with the integrator. If the servo can not reach the commanded position then the integrator will ramp up the motor current and force forever trying to drive the error to zero (often referred to as integrator wind up). You can limit this problem by reducing the Max Integrator value. Also after the hard stop is detected and the motor is pressing hard into the stop we should command a move to a position slightly away from the hard stop.
You can observe how all this works using the Step Response screen. Command a similar move that hits the hard
stop. You can then plot and view everything. Following error, motor current, Output ramping up, and so forth.
Regards
TK